package com.dstz.bpm.api.engine.action.button;

import com.dstz.base.core.util.AppUtil;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.constant.TaskType;
import com.dstz.bpm.api.engine.action.handler.ActionHandler;
import com.dstz.bpm.api.engine.data.result.BpmFlowData;
import com.dstz.bpm.api.engine.data.result.BpmFlowTaskData;
import com.dstz.bpm.api.model.def.NodeProperties;
import com.dstz.bpm.api.model.nodedef.BpmNodeDef;
import com.dstz.bpm.api.model.nodedef.Button;
import com.dstz.bpm.api.model.task.IBpmTask;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ButtonFactory {
   public static List<Button> generateButtons(BpmNodeDef nodeDef, boolean isDefault) throws ClassNotFoundException {
      NodeProperties prop = nodeDef.getNodeProperties();
      List<Button> btns = new ArrayList();
      Map<String, ActionHandler> actionMap = AppUtil.getImplInstance(ActionHandler.class);
      List<ActionHandler> list = new ArrayList(actionMap.values());
      sortActionList(list);
      Iterator var6 = list.iterator();

      while(true) {
         ActionHandler actionHandler;
         do {
            if (!var6.hasNext()) {
               return btns;
            }

            actionHandler = (ActionHandler)var6.next();
         } while(isDefault && !actionHandler.isDefault());

         if (actionHandler.isSupport(nodeDef)) {
            ActionType actionType = actionHandler.getActionType();
            Button button = new Button(actionType.getName(), actionType.getKey(), actionHandler.getDefaultGroovyScript(), actionHandler.getConfigPage());
            button.setBeforeScript(actionHandler.getDefaultBeforeScript());
            btns.add(button);
         }
      }
   }

   private static void sortActionList(List<ActionHandler> list) {
      Collections.sort(list, new Comparator<ActionHandler>() {
         public int compare(ActionHandler o1, ActionHandler o2) {
            return o1.getSn() - o2.getSn();
         }
      });
   }

   public static List<Button> getInstanceButtons() {
      List<Button> btns = new ArrayList();
      Map<String, ActionHandler> actionMap = AppUtil.getImplInstance(ActionHandler.class);
      List<ActionHandler> list = new ArrayList(actionMap.values());
      Iterator var3 = list.iterator();

      while(true) {
         ActionHandler actionHandler;
         ActionType actionType;
         do {
            if (!var3.hasNext()) {
               return btns;
            }

            actionHandler = (ActionHandler)var3.next();
            actionType = actionHandler.getActionType();
         } while(ActionType.FLOWIMAGE != actionType && ActionType.PRINT != actionType && ActionType.TASKOPINION != actionType && actionType != ActionType.REMINDER);

         Button button = new Button(actionType.getName(), actionType.getKey(), actionHandler.getDefaultGroovyScript(), actionHandler.getConfigPage());
         button.setBeforeScript(actionHandler.getDefaultBeforeScript());
         btns.add(button);
      }
   }

   public static List<Button> specialTaskBtnHandler(List<Button> btns, BpmFlowData flowData) {
      if (!(flowData instanceof BpmFlowTaskData)) {
         return btns;
      } else {
         IBpmTask task = ((BpmFlowTaskData)flowData).getTask();
         String name = task.getTaskType() + "ButtonChecker";
         if (TaskType.ADD_SIGN.equalsWithKey(task.getTaskType())) {
            name = "SIGNButtonChecker";
         }

         ButtonChecker checker = (ButtonChecker)AppUtil.getBean(name);
         if (checker == null) {
            return btns;
         } else {
            List<Button> buttons = new ArrayList();
            Iterator var6 = btns.iterator();

            while(var6.hasNext()) {
               Button btn = (Button)var6.next();
               if (checker.isSupport(btn)) {
                  buttons.add(btn);
               }
            }

            checker.specialBtnHandler(buttons);
            return buttons;
         }
      }
   }
}
